package com.zzptc.mzb.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzptc.mzb.myapplication.Bean.User;
import com.zzptc.mzb.myapplication.Frament.BlankFragment1;
import com.zzptc.mzb.myapplication.Frament.SeccndFragment;
import com.zzptc.mzb.myapplication.Frament.ThirdBlankFragment;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView menutext1,menutext2,menutext3,menutext4,menuuserid;
    private DrawerLayout main;
    private LinearLayout main_left;
    private ImageView menupic;
    private Button biao,xin,xiu;
    private BlankFragment1 blankFragment1=new BlankFragment1();
    private  SeccndFragment seccndFragment=new SeccndFragment();
    private ThirdBlankFragment thirdBlankFragment=new ThirdBlankFragment();
    String mta="#81DAF5";
    String ban="#1E90FF";
    String qan="#CEE3F6";
    String abc="#F7F8E0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceFragment(blankFragment1);//传数据
        getUser();//测试数据库添加
        init();//按键实例化
        setListenr();//点击事件，切换到其他建面
    }
    private void getUser() {
        List<User> userList= LitePal.findAll(User.class);
        if (userList.size()==0){
            //创建数据
            User user=new User(0,"小孟同学","520");
            User user1=new User(0,"阿月","1314");
            User user2=new User(0,"阿梅","7758");
            User user3=new User(0,"阿彬","831");
            user.save();//存储
            user1.save();
            user2.save();
            user3.save();
        }
    }
//监听器方法，    相同的功能的按钮的监听器
    private void setListenr() {
       View.OnClickListener listener= new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               switch (v.getId()){
                   case R.id.biao:
                   case R.id.menutext1:
                       ininColor(v);//点击变色
                       //v.setBackgroundColor(Color.parseColor("#1E90FF"));
                       v.setBackgroundColor(Color.parseColor(ban));
                       v.setBackgroundColor(Color.parseColor(qan));
                       //biao.setBackgroundColor(Color.parseColor("#DB7093"));
                       replaceFragment(blankFragment1);//实现转换到碎片上
                       break;
                   case R.id.xin:
                   case R.id.menutext2:
                       ininColor(v);//点击变色
                       v.setBackgroundColor(Color.parseColor(ban));
                       v.setBackgroundColor(Color.parseColor(qan));
                       //v.setBackgroundColor(Color.parseColor("#1E90FF"));
                      // xin.setBackgroundColor(Color.parseColor("#DB7093"));
                       replaceFragment(seccndFragment);
                       break;
                   case R.id.xiu:
                   case R.id.menutext3:
                       ininColor(v);//点击变色
                       v.setBackgroundColor(Color.parseColor(ban));
                       v.setBackgroundColor(Color.parseColor(qan));
                       //v.setBackgroundColor(Color.parseColor("#1E90FF"));
                    //  xiu.setBackgroundColor(Color.parseColor("#DB7093"));
                       replaceFragment(thirdBlankFragment);
                       break;
                   case R.id.menutext4:
                       Intent intent=new Intent(MainActivity.this,LoginMain2Activity.class);
                       startActivity(intent);
                       finish();
                       break;
               }
               main.closeDrawer(main_left);
           }
       };
       biao.setOnClickListener(listener);
        xin.setOnClickListener(listener);
        xiu.setOnClickListener(listener);


        menutext1.setOnClickListener(listener);
        menutext2.setOnClickListener(listener);
        menutext3.setOnClickListener(listener);
        menutext4.setOnClickListener(listener);
    }
    private void init() {
        main=findViewById(R.id.mz);
        main_left=findViewById(R.id.main_left);

        menutext1=findViewById(R.id.menutext1);
        menutext2=findViewById(R.id.menutext2);
        menutext3=findViewById(R.id.menutext3);
        menutext4=findViewById(R.id.menutext4);
        menupic=findViewById(R.id.menupic);
        menuuserid=findViewById(R.id.menuuserid);

        biao=findViewById(R.id.biao);
        xin=findViewById(R.id.xin);
        xiu=findViewById(R.id.xiu);
        ininColor(biao);//颜色的设置，带参数
        biao.setBackgroundColor(Color.parseColor(ban));
        xin.setBackgroundColor(Color.parseColor(mta));



    }
    private  void  ininColor( View v1){//RGB调理颜色，，也有不带参数就不用加view v1
        biao.setBackgroundColor(Color.parseColor(ban));
        xin.setBackgroundColor(Color.parseColor(ban));
        xiu.setBackgroundColor(Color.parseColor(ban));
        v1.setBackgroundColor(Color.parseColor(ban));//带参数添加


        menutext1.setBackgroundColor(Color.parseColor(mta));
        menutext2.setBackgroundColor(Color.parseColor(mta));
        menutext3.setBackgroundColor(Color.parseColor(mta));
        menutext4.setBackgroundColor(Color.parseColor(mta));
    }
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager=getSupportFragmentManager();//碎片管理员
        FragmentTransaction transaction=fragmentManager.beginTransaction();//开启事务
        transaction.replace(R.id.fragment_con,fragment);//替换事务 把fragment替换到fragment_con里面来
        transaction.commit();//提交
    }
}
